/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.clinicplus.service;

import br.com.foxline.clinicplus.entity.Doctor;
import br.com.foxline.clinicplus.entity.Unattended;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.Query;

/**
 *
 * @author roniere
 */
@Stateless
public class DoctorService extends GenericService<Doctor> {

    public DoctorService() {
        super(Doctor.class);
    }
    
    public List<Doctor> autocomplete(String doc) {
        List<Doctor> doctors = new ArrayList<Doctor>();
        
        String sql = "SELECT d FROM Doctor d WHERE ";
        
        if ((doc != null) && !(doc.equals(""))) {
            sql += " lower(d.name) like lower(:name) and  ";
        }
        
        sql += " 1=1 and d.active = true ORDER BY d.name ASC";
       
        Query query = getEntityManager().createQuery(sql);
        
        if ((doc != null) && (doc!= "")) {
            query.setParameter("name", "%" + doc + "%");
        }
        
        doctors = query.getResultList();
        return doctors;
    }
    
     public List<Doctor> findDoctor(Doctor doc) {
        List<Doctor> doctors = new ArrayList<Doctor>();
        
        String sql = "SELECT d FROM Doctor d WHERE ";
        
        if ((doc.getName() != null) && (!doc.getName().equals(""))) {
            sql += " lower(d.name) like lower(:name) and  ";
        }
        
//        if ((doc.get != "") && (doc.getOccupation() != null)) {
//            sql += " lower(c.cpf) like lower(:cpf) and  ";
//        }
        
        sql += " 1=1  and d.active = true ORDER BY d.name ASC ";
       
        Query query = getEntityManager().createQuery(sql);
        
        if ((doc.getName() != null) && (!doc.getName().equals(""))) {
            query.setParameter("name", "" + doc.getName() + "%");
        }
//        if ((customer.getCpf() != "") && (customer.getCpf() != null)) {
//            query.setParameter("cpf", "%" + customer.getCpf() + "%");
//        }
        
        doctors = query.getResultList();
        return doctors;
    }
     
  

    
}
